//
//  YTDriverDetailViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/20.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTDriverDetailViewController.h"
#import "YTSelectCarViewController.h"
#import "YTDriverTeamServices.h"
#import "YTDriverDetailModel.h"
#import "YTDriverCarModel.h"
#import "YTDrvierOrderViewController.h"

@interface YTDriverDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextField *driverNumText;
@property (weak, nonatomic) IBOutlet UITextField *driverNameText;
@property (weak, nonatomic) IBOutlet UITextField *driverTelText;
@property (weak, nonatomic) IBOutlet UITextField *driverTel2Text;
@property (weak, nonatomic) IBOutlet UITextField *driverWechatText;
@property (weak, nonatomic) IBOutlet UILabel *driverCarInfoText;
@property (weak, nonatomic) IBOutlet UILabel *driverAllOrdersText;
@property (weak, nonatomic) IBOutlet UILabel *driverCompletionText;
@property (weak, nonatomic) IBOutlet UILabel *driverUncompletionText;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *updateBtn;
@end

@implementation YTDriverDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"driverId"] = _driver_id;
    [SVProgressHUD show];
    [[YTDriverTeamServices loadDriverDetail:dict] subscribeNext:^(NSDictionary *response) {
        [self loadDeital:[YTDriverDetailModel yy_modelWithJSON:response[@"responseData"]]];
    }];
    
    /// 点击更新按钮，这时候按钮变成完成
    /// 监听更新按钮的变化，然后绑定在textfield上
    RACSignal *enableSignal = [RACObserve(_updateBtn, title) map:^id(NSString *title) {
        return @([title isEqualToString:@"更新"] ? 0 : 1);
    }];
    RAC(_driverNameText, enabled) = enableSignal;
    RAC(_driverTelText, enabled) = enableSignal;
    RAC(_driverTel2Text, enabled) = enableSignal;
    RAC(_driverWechatText, enabled) = enableSignal;
}

- (void)loadDeital:(YTDriverDetailModel *)model {
    [SVProgressHUD dismiss];
    _driverNumText.text = model.driverNumber;
    _driverNameText.text = model.contactName;
    _driverTelText.text = model.mobile;
    _driverTel2Text.text = model.mobile2;
    _driverWechatText.text = model.wechat;
    _driverCarInfoText.text = model.carInfoName.length ? model.carInfoName : @"添加车辆";
    _driverAllOrdersText.text = model.allOrderNumber;
    _driverCompletionText.text = model.finishedOrderNumber;
    _driverUncompletionText.text = model.noFinishedOrderNumber;
    
    if (model.carInfoName.length) {
        _driverCarInfoText.textColor = [UIColor blackColor];
    }
}

- (IBAction)updateAction:(UIBarButtonItem *)sender {
    if ([sender.title isEqualToString:@"完成"]) {
        [self uploadUpdateData];
    }
    
    sender.title = ([sender.title isEqualToString:@"更新"] ? @"完成" : @"更新");
}

#pragma mark - 网络请求

- (void)uploadUpdateData {
    [SVProgressHUD show];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"realName"] = _driverNameText.text;
    dict[@"phone"] = _driverTelText.text;
    dict[@"phone2"] = _driverTel2Text.text;
    dict[@"wechat"] = _driverWechatText.text;
    dict[@"driverId"] = _driver_id;
    [[YTDriverTeamServices updateDriverDetail:dict] subscribeNext:^(id x) {
        [SVProgressHUD dismiss];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)addCarToDriver:(NSString *)car_id {
    [SVProgressHUD show];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"driverId"] = _driver_id;
    dict[@"carId"] = car_id;
    [[YTDriverTeamServices addCarToDriver:dict] subscribeNext:^(id x) {
        [SVProgressHUD showSuccessWithStatus:@"添加车辆成功!"];
    }];
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YTSelectCar"]) {
        YTSelectCarViewController *carView = segue.destinationViewController;
        carView.callBack = [RACSubject subject];
        @weakify(self);
        [carView.callBack subscribeNext:^(YTDriverCarModel *model) {
            @strongify(self);
            [self addCarToDriver:model.carId];
            self.driverCarInfoText.text = model.carTitle;
            self.driverCarInfoText.textColor = [UIColor blackColor];
        }];
    } else {
        YTDrvierOrderViewController *driverOrder = segue.destinationViewController;
        if ([segue.identifier isEqualToString:@"YTDriverAllOrder"]) {
            driverOrder.title = @"全部订单";
            driverOrder.order_type = @"0";
        } else if ([segue.identifier isEqualToString:@"YTDriverUncompleteOrder"]) {
            driverOrder.title = @"待完成";
            driverOrder.order_type = @"2";
        } else {
            driverOrder.title = @"已完成";
            driverOrder.order_type = @"3";
        }
        driverOrder.driver_id = self.driver_id;
    }
}

@end
